var _ = require('lodash'),
    Q = require('q'),
    mongoose = require('mongoose'),
    entityCollection = require('../../../Base/server/controllers/EntityCollection'),
    BarcodeEnum = require('../../../Barcode/server/controllers/Enum.server.controller'),
    Transaction = require('../../../Base/server/controllers/Transaction'),
    PublicEnum = require('../../../Base/server/controllers/Enum'),
    workLocationController = require('../../../CBO/server/controllers/WorkLocation.server.controller');

// 增加异常流水
// bindContext包含字段：Work、WorkName、WorkLacation、WorkLacationName、User
exports.writeProcessException = function (barcodeMaster, bindContext, exceptionType, missingOperations) {
    var deferred = Q.defer();

    workLocationController.findById(
        bindContext.WorkLocation, { Department: 1 }, { path: 'Department', select: 'Name' }
    ).then(function (workLocationRecord) {
        if (!workLocationRecord) {
            var newErr = new Error();
            newErr.leval = 9;
            newErr.title = '查询工位错误';
            newErr.message = '没有找到对应工位';
            deferred.reject(newErr);
            return;
        }
        var records = [];
        if (exceptionType === BarcodeEnum.ExceptionType.DrainScard &&
            missingOperations && missingOperations.length > 0
        ) {
            _.forEach(missingOperations, function (operation) {
                records.push({
                    RowStatus: PublicEnum.RowStatus.Insert,
                    CreatedBy: bindContext.User,
                    Barcode: barcodeMaster.Barcode,
                    ItemMasterCode: barcodeMaster.ItemMasterCode,
                    ItemMasterName: barcodeMaster.ItemMasterName,
                    QCStatus: barcodeMaster.QCStatus,
                    DepartmentName: workLocationRecord.Department.Name,
                    WorkLocationName: bindContext.WorkLocationName,
                    WorkName: bindContext.WorkName,
                    ExceptionType: BarcodeEnum.ExceptionType.DrainScard,
                    MissingWorkName: operation.Work.Name,
                    IsRead: false,
                    DescSeg1: barcodeMaster.DescSeg1,
                    DescSeg2: barcodeMaster.DescSeg2
                });
            });
        } else {
            records.push({
                RowStatus: PublicEnum.RowStatus.Insert,
                CreatedBy: bindContext.User,
                Barcode: barcodeMaster.Barcode,
                ItemMasterName: barcodeMaster.ItemMasterName,
                QCStatus: barcodeMaster.QCStatus,
                DepartmentName: workLocationRecord.Department.Name,
                WorkLocationName: bindContext.WorkLocationName,
                WorkName: bindContext.WorkName,
                ExceptionType: exceptionType,
                IsRead: false,
                DescSeg1: barcodeMaster.DescSeg1,
                DescSeg2: barcodeMaster.DescSeg2
            });
        }
        Transaction.BatchSaveByTran([
            { EntityName: 'ProcessException', Records: records }
        ], function (err, result) {
            if (err) {
                deferred.reject(err);
                return;
            }
            deferred.resolve(result);
        });
    });

    return deferred.promise;
};